<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
$process_unit = sxREQ("pu");
if($process_unit == ""){	
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("missing cod. 'pu'"));
	serverRedir($url);
}

if(sxREQ("reqtype") == "save"){
	$errMsg .= puMod();		
}
$sSQL = "SELECT p.*, f.functional_area, m.description AS m_des, f.description AS f_des, s.description AS s_des "
				." FROM process_unit p " 
				." LEFT JOIN module m ON m.module = p.module "
				." LEFT JOIN sub_module s ON s.module = p.module AND s.sub_module = p.sub_module"
				." LEFT JOIN functional_area f ON f.module = p.module AND f.functional_area = s.functional_area "
				." WHERE p.process_unit = '".$process_unit."'";
$objSDAO->query($sSQL, "mod");
if(!$row = $objSDAO->fetch("mod")){
	$errMsg .= _t("process-unit '%s' not found",$process_unit)."<br>";
}
sxRDef('sub', substr($process_unit,5,4));
sxRDef('new_name', substr($process_unit,10));
sxRDef('description', $row['description']);
sxRDef('type', $row['type']);
sxRDef('note', $row['note']);

$sSQL = "SELECT s.sub_module, s.description FROM sub_module s "
	." WHERE s.module = '".substr($process_unit,0,4)."' ";
$sSQL .= " ORDER BY s.sub_module";
$objSDAO->query($sSQL, "pu");
$aSub = array(''=>'-- select --');
while($row = $objSDAO->fetch("pu")) {
	$aSub[$row['sub_module']] = $row['sub_module']." - ".$row['description'];
}

//process docs
$sSQL = "SELECT * FROM process_unit_doc WHERE process_unit='$process_unit'";
$objSDAO->query($sSQL, "doc");

$aOpz = array("kMenu"=>'pro.pru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
<?php

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('pu');

echo getSubHeader(substr($process_unit,0,4),substr($process_unit,5,4));

$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>Process-unit</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Sub-module (*)</td>" .
			"<td class=\"text01\">".getHtmlSelect('sub', $aSub)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Name(*)</td>" .
			"<td class=\"text01\">".getHtmlText('new_name',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\">".getHtmlText('description',80,80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Type</td>" .
			"<td class=\"text01\">");
$aType["web-page-reloadable"] = "web-page-reloadable";
$aType["web-page-unreloadable"] = "web-page-unreloadable";
echo getHtmlSelect('type',$aType);
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\">".getHtmlTextArea('note',80,3)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Document</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"doc_name\" id=\"doc_name\" maxlength=\"80\" size=\"32\" value=\"$doc_name\">&nbsp;&nbsp;<input class=\"inpt01\" type=\"file\" name=\"document\" id=\"document\"></td>" .
		"</tr>\n");
echo("</table>");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan=\"2\">Documents</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"tabh01\" width=\"90%\">File</td>" .
			"<td class=\"tabh01\" width=\"90%\">Delete</td>" .
		"</tr>\n");
$numDoc = 0;
while($row = $objSDAO->fetch("doc")){
	$numDoc++;
	echo("<tr>" .
			"<td class=\"text01\"><a class=\"text01\" target=\"blank\" href=\"getDoc.php?pu=$process_unit&fn=".$row["file_name"]."&xsn=".XSN."\">".sxHE($row["name"])."</a></td>" .		
			"<td class=\"text01\">" .
			"<input class=\"inpt01\" type=\"checkbox\" name=\"elim_$numDoc\" id=\"elim_$numDoc\">" .
			"<input type=\"hidden\" name=\"fn_elim_$numDoc\" id=\"fn_elim_$numDoc\" value=\"".$row["file_name"]."\">" .
			"</td>" .
		"</tr>\n");
}
echo("</table>");
echo $sCmd;

echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function puMod(){
	global $objSDAO;
	$errMsg = "";	
	$process_unit = sxREQ('pu');
	$module = substr($process_unit,0,4);
	$sub_module = substr($process_unit,5,4);
	if($process_unit == "")
		$errMsg .= "missing process-unit code<br>";
	if(preg_match("/[^a-zA-Z0-9_]/",sxREQ("new_name")))
		$errMsg .= "Name: letter, number and _ only<br>";
	if(sxRTrim("new_name") == "")
		$errMsg .= "missing name<br>";
	if(sxRTrim("sub") == "")
		$errMsg .= "missing sub-module<br>";
	if(trim($_REQUEST["description"]) == "")
		$errMsg .= "missing description<br>";
	
	$new_process_unit = $module.".".sxREQ("sub").".".sxREQ("new_name");
	if ($new_process_unit != $process_unit) {
		$sSQL = "SELECT process_unit FROM process_unit " .
				"	WHERE process_unit='".sxAS($new_process_unit)."'";
		$num = 0;
		$objSDAO->query($sSQL,"test");
		if($row)
			$errMsg .= "existing name<br>";
	}	
	if($errMsg != ""){
		return $errMsg;
	}
	$sSQL = "UPDATE process_unit SET sub_module = '".sxRAS("sub")."', ".
			"description='".sxRAS("description")."', " .
			"type='".sxRAS("type")."', " .
			"note='".sxRAS("note")."'" .
			"WHERE process_unit='".sxRAS("pu")."'";	
	$objSDAO->query($sSQL, "mod");
	if ($new_process_unit != $process_unit) {
		$aTabDip['entry_point'][] = 'process_unit';
		$aTabDip['entry_point_alias'][] = 'process_unit';
		$aTabDip['entry_point_alias_match'][] = 'process_unit';
		$aTabDip['entry_point_attribute'][] = 'process_unit';
		$aTabDip['entry_point_rule'][] = 'process_unit';
		$aTabDip['entry_point_rule_attribute'][] = 'process_unit';
		$aTabDip['entry_point_rule_sub_rule'][] = 'process_unit';
		$aTabDip['entry_point_variable'][] = 'process_unit';
		$aTabDip['frame'][] = 'process_unit';
		$aTabDip['frame_alias_match'][] = 'process_unit';
		$aTabDip['menu_line'][] = 'process_unit';
		$aTabDip['process_unit_doc'][] = 'process_unit';
		$aTabDip['pu_alias'][] = 'process_unit';
		$aTabDip['pu_attribute'][] = 'process_unit';
		$aTabDip['pu_link'][] = 'process_unit';
		$aTabDip['pu_variable'][] = 'process_unit';
		$aTabDip['process_unit'][] = 'process_unit';
		$aTabDip['pu_link'][] = 'pu_linked';
		//rename all tables
		foreach($aTabDip as $tabX => $aNomeX) {
			foreach($aNomeX as $nomeX) {
				$sSQL = "UPDATE $tabX SET $nomeX = '$new_process_unit' WHERE $nomeX = '$process_unit'";
				//$errMsg .= $sSQL."<br>";
				$objSDAO->exec($sSQL);
			}
		}
	}
	$module = sxREQUEST("module");
	if(!deleteDoc($process_unit, $errMsg) or !insertDoc($process_unit, $errMsg)){
		$errMsg .= "document load failed<br>";
	}
	if ($errMsg != '') {
		return $errMsg;
	}
	$_SESSION[USEROBJ]->setHistoryVar(array('pu'=>$new_process_unit));
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return "redirect error";
}
